/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：AbstractModel.js<br>
 * 日期：2016年9月19日<br>
 * 作者: 王志远<br>
 * 功能：抽象模型<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.AbstractModel<br>
 * 类型功能：抽象模型<br>
 */
Rainier.UI.V2.AbstractModel = Rainier.Basic.CompositeObject.extend({
	
	NAME : "Rainier.UI.V2.AbstractModel",
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.init<br>
	 * 函数功能：构造函数<br>
	 * 参数输入:property属性对象<br>
	 * 异常信息：无<br>
	 */
	init : function(property){
		this._super();
		this.property = property;
		this.duplicate = null;
	},
	
	
	/**
	 * 函数名称：Rainier.UI.V2.CompositeModel.serializeToJson<br>
	 * 函数功能：将对象串行化为Json对象<br>
	 * 函数返回：json对象<br>
	 * 异常信息：无<br>
	 */
	serializeToJson : function(){
		var json  = {};
		json.type = this.NAME;
		json.uuid = this.getUuid();
		json.uiProperty 	= this.property.uiProperty ? $.extend(true, {}, this.property.uiProperty) : {};
		json.modelProperty 	= this.property.modelProperty ? $.extend(true, {}, this.property.modelProperty) : {};
		return json;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.clone<br>
	 * 函数功能：对象拷贝<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：Rainier.UI.V2.AbstractModel<br>
	 * 异常信息：无<br>
	 */
	clone : function(){
		if(!this.duplicate){
			this.duplicate = eval('new '+this.NAME+'(this.property)');// Rainier.UI.V2.AbstractModel();
		}
		// var uuid  = this.duplicate.getUuid();
		// $.extend(this.duplicate,this);
		// this.duplicate.setUuid(uuid);
		
		return this.duplicate;
	},
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.clearDuplicate<br>
	 * 函数功能：清除拷贝对象<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	clearDuplicate : function(){
		this.duplicate = null;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.getProperty<br>
	 * 函数功能：获取属性对象<br>
	 * 函数返回：Object<br>
	 * 异常信息：<br>
	 */
	getProperty : function(){
		return this.property;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.active<br>
	 * 函数功能：模型激活<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	active : function(omittedListener){
		var oldValue = {active:this.property.active,model:this};
		this.property.active = true;
		var newValue = {active:this.property.active,model:this};
		var event1 = Rainier.UI.V2.AbstractModel.MODEL_EVENT_ACTIVE_STATE_CHANGED;
		this.firePropertyChange(
				event1, 
				oldValue, 
				newValue,
				omittedListener
		);
		var event2 = Rainier.UI.V2.EditorObjectSelectionChangeListener.SELECTION_CHANGE_EVENT;
		this.firePropertyChange(
				event2, 
				oldValue, 
				newValue,
				omittedListener
		);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.deactive<br>
	 * 函数功能：模型失活<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	deactive : function(omittedListener){
		var oldValue = {active:this.property.active,model:this};
		this.property.active = false;
		var newValue = {active:this.property.active,model:this};
		var event = Rainier.UI.V2.AbstractModel.MODEL_EVENT_ACTIVE_STATE_CHANGED;
		this.firePropertyChange(
				event, 
				oldValue, 
				newValue,
				omittedListener
		);
		
		var event2 = Rainier.UI.V2.EditorObjectSelectionChangeListener.SELECTION_CHANGE_EVENT;
		this.firePropertyChange(
				event2, 
				oldValue, 
				newValue,
				omittedListener
		);
	},
		
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.setDimension<br>
	 * 函数功能：设置尺寸<br>
	 * 参数输入：width:Number<br>
	 * 参数输入：height:Number<br>
	 * 参数输入：needCommandRequest:Boolean<br>
	 * 参数输入：omittedListener：忽略的侦听器<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setDimension : function(width,height,needCommandRequest,omittedListener){
		if(this.property){
			if(this.property.uiProperty){
				var oldValue = {};
				oldValue.width  = this.property.uiProperty.width;
				oldValue.height = this.property.uiProperty.height;
				
				var newValue = {};
				newValue.width  = width;
				newValue.height = height;
				newValue.needCommandRequest = needCommandRequest;
				
				this.property.uiProperty.width  = width;
				this.property.uiProperty.height = height;
				
				var event = Rainier.UI.V2.AbstractModel.MODEL_EVENT_DIMENSION_CHANGED;
				this.firePropertyChange(
						event, 
						oldValue, 
						newValue,
						omittedListener
				);								
			}
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.setPosition<br>
	 * 函数功能：设置Model位置<br>
	 * 参数输入：x:Number<br>
	 * 参数输入：y:Number<br>
	 * 参数输入：needCommandRequest:Boolean<br>
	 * 参数输入：omittedListener：忽略的侦听器<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setPosition : function(x,y,needCommandRequest,omittedListener){
		if(this.property){
			if(this.property.uiProperty){
				var oldValue = {};
				oldValue.x = this.property.uiProperty.x;
				oldValue.y = this.property.uiProperty.y;
				
				var newValue = {};
				newValue.x = x;
				newValue.y = y;
				newValue.needCommandRequest = needCommandRequest;
				
				this.property.uiProperty.x = x;
				this.property.uiProperty.y = y;
				if(this.property.modelProperty.locator){
					this.property.modelProperty.locator.x = x;
					this.property.modelProperty.locator.y = y;
				}
				
				var event = Rainier.UI.V2.AbstractModel.MODEL_EVENT_POSITION_CHANGED;
				this.firePropertyChange(
						event, 
						oldValue, 
						newValue,
						omittedListener
				);								
			}
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.setUiProperty<br>
	 * 函数功能：设置界面属性值<br>
	 * 参数输入：sPropertyName:String<br>
	 * 参数输入：value:Object<br>
	 * 参数输入：needCommandRequest:Boolean<br>
	 * 参数输入：omittedListener：忽略的侦听器<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setUiProperty : function(sPropertyName,value,needCommandRequest,omittedListener){
		if(this.property){
			if(this.property.uiProperty){
				var oldValue = jQuery.extend(true,{},this.property.uiProperty);
				oldValue.width  = this.property.uiProperty.width;
				oldValue.height = this.property.uiProperty.height;
				
				this.property.uiProperty[sPropertyName] = value;
				
				var newValue = this.property.uiProperty;
				newValue.needCommandRequest = needCommandRequest;
				
				var event = Rainier.UI.V2.AbstractModel.MODEL_EVENT_UI_PROPERTY_CHANGED;
				this.firePropertyChange(
						event, 
						oldValue, 
						newValue,
						omittedListener
				);								
			}
		}
	},
		
	/**
	 * 函数名称：Rainier.UI.V2.AbstractModel.setProperty<br>
	 * 函数功能：设置参数值<br>
	 * 参数输入：propertyKey:String<br>
	 * 参数输入：propertyValue：Object<br>
	 * 参数输入：needCommandRequest：Boolean<br>
	 * 参数输入：omittedListener：忽略的侦听器<br>
	 * 函数返回：<br>
	 * 异常信息：<br>
	 */
	setProperty : function(propertyObj,propertyKey,propertyValue,needCommandRequest,omittedListener){
		var oldValue = propertyObj[propertyKey];
		propertyObj[propertyKey] = propertyValue;
		
		var objOld = {};
		objOld.object = propertyObj;
		objOld.property = propertyKey.toString();
		objOld.value = oldValue;
		
		var objNew = {};
		objNew.object = propertyObj;
		objNew.property = propertyKey.toString();
		objNew.value = propertyValue;
		objNew.needCommandRequest = needCommandRequest;
		
		var event = Rainier.UI.V2.AbstractModel.MODEL_EVENT_PROPERTY_VALUE_CHANGED;
		this.firePropertyChange(
				event, 
				objOld, 
				objNew,
				omittedListener
		);			
	}
});

Rainier.UI.V2.AbstractModel.MODEL_EVENT_UI_PROPERTY_CHANGED     = "model event ui property changed";	//UI界面属性变化事件
Rainier.UI.V2.AbstractModel.MODEL_EVENT_ACTIVE_STATE_CHANGED    = "model event active state changed";	//激活状态变化事件 
Rainier.UI.V2.AbstractModel.MODEL_EVENT_PROPERTY_VALUE_CHANGED 	= "model event property value changed"; //参数值变化事件
Rainier.UI.V2.AbstractModel.MODEL_EVENT_POSITION_CHANGED 		= "model event position changed"; 		//位置变化
Rainier.UI.V2.AbstractModel.MODEL_EVENT_DIMENSION_CHANGED 		= "model event dimension changed"; 		//尺寸变化
